﻿using System;

namespace Simp.CodeModel.CoreFunctions
{
	/// <summary>Encapsulates a square root function.</summary>
	public class SqrtFunction : IFunction
	{
		private static readonly string[] PARAMETER_NAMES = new[] { "a", "b" };
		private static readonly IFunctionParameterBinding[] DEFAULT_BINDING =
			new[] { FunctionHelper.FunctionParameterBindingFactory.Build("b", new IntegerFunction(2)) };

		#region IFunction Members
		string IFunction.FunctionName
		{
			get { return "Sqrt"; }
		}

		string[] IFunction.ParameterNames
		{
			get { return PARAMETER_NAMES; }
		}

		IFunctionParameterBinding[] IFunction.DefaultParameterValues
		{
			get { return DEFAULT_BINDING; }
		}

		IFunction IFunction.Simplify(SimplificationMode simplificationMode, params IFunction[] parameters)
		{
			IFunction a = parameters[0];
			IFunction b = parameters[1];
			IntegerFunction bInteger = b as IntegerFunction;

			if (bInteger == null)
			{
				if (bInteger.Value == 1)
				{
					return a;
				}
			}

			IFunction oneOverB = new BoundFunctionCalling(new DivisionFunction(), new IntegerFunction(1), b);

			return new BoundFunctionCalling(new PowerFunction(), a, oneOverB);
		}

		IFunction IFunction.AcceptVisitor(IFunctionVisitor visitor)
		{
			return null;
		}
		#endregion
	}
}